#![allow(unused_imports,dead_code,unused_assignments)]
use axum::{
    extract::{Form, Query, Json, Multipart,DefaultBodyLimit},
    routing::{get,post},
    response::Response,
    body::{Body, Bytes},
    Router
};
use crate::domain::GoodsBean;
use crate::domain::GoodsQueryRequest;
use crate::serviceimpl::goods_service::get_scroll_goodses as service_get_scroll_goodses;
use crate::serviceimpl::goods_service::get_goods_list as service_get_goods_list;
use serde::{Deserialize};
use serde_json::{Value,Map};
use std::collections::HashMap;
use std::fs;
use std::sync::Mutex;
use tower_http::{trace::TraceLayer};


lazy_static! {
	static ref TEMP_GOODS_IMAGES:Mutex<HashMap<String,String>> = Mutex::new(HashMap::new());
}

pub fn store_goods_picture(key:String,photo:String) {
	TEMP_GOODS_IMAGES.lock().unwrap().insert(key,photo);
}

pub fn pick_goods_picture(key:String) ->String {
	let ret:Option<String> = TEMP_GOODS_IMAGES.lock().unwrap().get(&key).cloned();
	TEMP_GOODS_IMAGES.lock().unwrap().insert(key,"".to_string());
	if ret.is_none() {
		return "".to_string();
	}
	ret.as_ref().unwrap().to_string()
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GoodsRequest {
	pub id: Option<i64>,
	pub name: Option<String>,
	pub active: Option<bool>,
	pub price: Option<f64>,
	pub intro: Option<String>,
	pub stock: Option<i32>,
	pub goods_type_id: Option<i64>,
	pub is_scroll: Option<bool>,
	pub is_hot: Option<bool>,
	pub is_new: Option<bool>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IdsRequest {
	pub ids: String,
}

pub fn index_cust_controller() -> Router {
	Router::new()
	.route("/index", post(index))
	.layer(DefaultBodyLimit::max(2*1024*1024))
	.layer(TraceLayer::new_for_http())
}

pub async fn index() ->String {
	let scroll_goodses = service_get_scroll_goodses().await.unwrap();
	let new_goodses = service_get_goods_list(3).await.unwrap();
	let hot_goodses = service_get_goods_list(2).await.unwrap();
	let scroll_json = serde_json::to_string_pretty(&scroll_goodses).unwrap();
	let new_json = serde_json::to_string_pretty(&new_goodses).unwrap();
	let hot_json = serde_json::to_string_pretty(&hot_goodses).unwrap();

	let mut map = Map::new();
	map.insert("success".to_string(), Value::from(true));
	map.insert(
		"scroll".to_string(),
		serde_json::from_str(&scroll_json).unwrap(),
	);
	map.insert(
		"newList".to_string(),
		serde_json::from_str(&new_json).unwrap(),
	);
	map.insert(
		"hotList".to_string(),
		serde_json::from_str(&hot_json).unwrap(),
	);
	let resultjson = serde_json::to_string_pretty(&map).unwrap();
	return resultjson;
}
